package io.github.idol.roomlist

import android.support.v7.recyclerview.extensions.ListAdapter
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import io.github.idol.data.RoomListInfo
import io.github.idol.databinding.ListItemRoomBinding


class RoomListAdapter(private val callback:RoomListAdapter.Callback): ListAdapter<RoomListInfo.ContentBean, RoomListAdapter.ViewHolder>(RoomDiffCallback()){

    interface Callback{
        fun onRoomClick(content:RoomListInfo.ContentBean)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val contentBean = getItem(position)
        Log.i("tag","position:$position content:${contentBean.comment}")
        holder.bind(callback,contentBean)
    }

    override fun onCreateViewHolder(parent: ViewGroup, position: Int): ViewHolder {
        return ViewHolder(ListItemRoomBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    class ViewHolder(
            private val binding: ListItemRoomBinding
    ):RecyclerView.ViewHolder(binding.root){
        fun bind(callback: Callback,item: RoomListInfo.ContentBean){
            binding.apply {
                eventHandler = callback
                contentBean = item
                executePendingBindings()
            }
        }
    }
}